package com.aqs;

import java.util.concurrent.*;

/**
 * @author: csh
 * @Date: 2022/12/5 22:20
 * @Description:BlockingQueue学习
 */
public class BlockingQueueTest {

    public static void main(String[] args) throws Exception {
        //由数组支持的有界队列
//        BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(16);
//        for (int i = 0; i < 100; i++) {
//            //超过容量会爆出异常
//            //arrayBlockingQueue.add("第"+i);
//            //超过容量会阻塞
//            //arrayBlockingQueue.put("第"+i);
//            //超过容量会返回false
//            //System.out.println(arrayBlockingQueue.offer("第"+i));
//            //带过期时间
//            arrayBlockingQueue.offer("第"+i,1000,TimeUnit.SECONDS);
//        }
        //由链接节点支持的可选有界队列
        BlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();
//        for (int i = 0; i < 100; i++) {
            //超过容量会爆出异常
            //linkedBlockingQueue.add("第"+i);
            //超过容量会阻塞
            //linkedBlockingQueue.put("第"+i);
            //超过容量会返回false
            //System.out.println(linkedBlockingQueue.offer("第"+i));
            //带过期时间
            //linkedBlockingQueue.offer("第"+i,1000,TimeUnit.SECONDS);
//        }
        //由优先级堆支持的无界优先级队列
        BlockingQueue<String> priorityBlockingQueue =new PriorityBlockingQueue();
        for (int i = 0; i < 1000; i++) {
            //超过容量会爆出异常
            //priorityBlockingQueue.add("第"+i);
            //超过容量会阻塞
            //priorityBlockingQueue.put("第"+i);
            //超过容量会返回false
            System.out.println(priorityBlockingQueue.offer("第"+i));
            //带过期时间
            //priorityBlockingQueue.offer("第"+i,1000,TimeUnit.SECONDS);
        }
        System.out.println(priorityBlockingQueue.take());
        System.out.println(priorityBlockingQueue.remainingCapacity());
        System.out.println(priorityBlockingQueue.remove());
        System.out.println(priorityBlockingQueue.poll());
//
//        //由优先级堆支持的、基于时间的调度队列
//        BlockingQueue<String> delayQueue =   new DelayQueue();
//        for (int i = 0; i < 100; i++) {
//            //超过容量会爆出异常
//            delayQueue.add("第"+i);
//            //超过容量会阻塞
//            //delayQueue.put("第"+i);
//            //超过容量会返回false
//            //System.out.println(arrayBlockingQueue.offer("第"+i));
//            //带过期时间
//            //priorityBlockingQueue.offer("第"+i,1000,TimeUnit.SECONDS);
//        }
    }
}
